using System;
using System.Collections;
using System.Xml;
using Altova.TypeInfo;
using Altova.Xml;
using Altova.Xml.Meta;

namespace RDML_v1_2_REC.rdml
{
	public class RDML_v1_2_REC2 : TypeBase
	{
		internal class Enumerator : EnumeratorBase
		{
			public override object Current
			{
				get
				{
					return new RDML_v1_2_REC2((XmlNode)base.InnerCurrent);
				}
			}

			public Enumerator(IEnumerator inner)
				: base(inner)
			{
			}
		}

		public class MemberElement_rdml2 : IEnumerable
		{
			private TypeBase owner;

			private MemberInfo info;

			public rdmlType this[int i]
			{
				get
				{
					return At(i);
				}
			}

			public rdmlType First
			{
				get
				{
					return new rdmlType(owner.GetElementFirst(info));
				}
			}

			public rdmlType Last
			{
				get
				{
					return new rdmlType(owner.GetElementLast(info));
				}
			}

			public bool Exists
			{
				get
				{
					return Count > 0;
				}
			}

			public int Count
			{
				get
				{
					return owner.CountElement(info);
				}
			}

			public Element Info
			{
				get
				{
					return new Element(info);
				}
			}

			public MemberElement_rdml2(TypeBase owner, MemberInfo info)
			{
				this.owner = owner;
				this.info = info;
			}

			public rdmlType At(int index)
			{
				return new rdmlType(owner.GetElementAt(info, index));
			}

			public rdmlType Append()
			{
				return new rdmlType(owner.CreateElement(info));
			}

			public void Remove()
			{
				owner.RemoveElement(info);
			}

			public void RemoveAt(int index)
			{
				owner.RemoveElementAt(info, index);
			}

			public IEnumerator GetEnumerator()
			{
				return new rdmlType.Enumerator(XmlTreeOperations.GetElements(owner.Node, info).GetEnumerator());
			}
		}

		public MemberElement_rdml2 rdml2;

		public static ComplexType StaticInfo
		{
			get
			{
				return new ComplexType(RDML_v1_2_REC_TypeInfo.binder.Types[RDML_v1_2_REC_TypeInfo._altova_ti_rdml_altova_RDML_v1_2_REC2]);
			}
		}

		public static RDML_v1_2_REC2 LoadFromFile(string filename)
		{
			return new RDML_v1_2_REC2(XmlTreeOperations.LoadDocument(filename));
		}

		public static RDML_v1_2_REC2 LoadFromString(string xmlstring)
		{
			return new RDML_v1_2_REC2(XmlTreeOperations.LoadXml(xmlstring));
		}

		public static RDML_v1_2_REC2 LoadFromBinary(byte[] binary)
		{
			return new RDML_v1_2_REC2(XmlTreeOperations.LoadXmlBinary(binary));
		}

		public void SaveToFile(string filename, bool prettyPrint)
		{
			XmlDocument xmlDocument = (XmlDocument)base.Node;
			if (xmlDocument.FirstChild is XmlDeclaration)
			{
				string encoding = ((XmlDeclaration)xmlDocument.FirstChild).Encoding;
				if (encoding == string.Empty)
				{
					XmlTreeOperations.SaveDocument(xmlDocument, filename, "UTF-8", false, false, prettyPrint);
				}
				else
				{
					XmlTreeOperations.SaveDocument(xmlDocument, filename, encoding, prettyPrint);
				}
			}
			else
			{
				XmlTreeOperations.SaveDocument(xmlDocument, filename, "UTF-8", false, false, prettyPrint);
			}
		}

		public void SaveToFile(string filename, bool prettyPrint, string encoding)
		{
			SaveToFile(filename, prettyPrint, encoding, string.Compare(encoding, "UTF-16BE", true) == 0, string.Compare(encoding, "UTF-16", true) == 0);
		}

		public void SaveToFile(string filename, bool prettyPrint, string encoding, bool bBigEndian, bool bBOM)
		{
			XmlDocument doc = (XmlDocument)base.Node;
			XmlTreeOperations.SaveDocument(doc, filename, encoding, bBigEndian, bBOM, prettyPrint);
		}

		public string SaveToString(bool prettyPrint)
		{
			XmlDocument doc = (XmlDocument)base.Node;
			return XmlTreeOperations.SaveXml(doc, prettyPrint);
		}

		public byte[] SaveToBinary(bool prettyPrint)
		{
			XmlDocument xmlDocument = (XmlDocument)base.Node;
			if (xmlDocument.FirstChild is XmlDeclaration)
			{
				string encoding = ((XmlDeclaration)xmlDocument.FirstChild).Encoding;
				if (encoding == string.Empty)
				{
					return XmlTreeOperations.SaveXmlBinary(xmlDocument, "UTF-8", false, false, prettyPrint);
				}
				return XmlTreeOperations.SaveXmlBinary(xmlDocument, encoding, prettyPrint);
			}
			return XmlTreeOperations.SaveXmlBinary(xmlDocument, "UTF-8", false, false, prettyPrint);
		}

		public byte[] SaveToBinary(bool prettyPrint, string encoding)
		{
			return SaveToBinary(prettyPrint, encoding, string.Compare(encoding, "UTF-16BE", true) == 0, string.Compare(encoding, "UTF-16", true) == 0);
		}

		public byte[] SaveToBinary(bool prettyPrint, string encoding, bool bBigEndian, bool bBOM)
		{
			XmlDocument doc = (XmlDocument)base.Node;
			return XmlTreeOperations.SaveXmlBinary(doc, encoding, bBigEndian, bBOM, prettyPrint);
		}

		public static RDML_v1_2_REC2 CreateDocument()
		{
			return CreateDocument("UTF-8");
		}

		public static RDML_v1_2_REC2 CreateDocument(string encoding)
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.AppendChild(xmlDocument.CreateXmlDeclaration("1.0", encoding, null));
			return new RDML_v1_2_REC2(xmlDocument);
		}

		public void SetDTDLocation(string dtdLocation)
		{
			XmlDocument xmlDocument = (XmlDocument)base.Node;
			string publicId = null;
			string internalSubset = null;
			if (xmlDocument.DocumentElement == null)
			{
				throw new InvalidOperationException("SetDTDLocation requires a root element.");
			}
			if (xmlDocument.DocumentType != null)
			{
				publicId = xmlDocument.DocumentType.PublicId;
				internalSubset = xmlDocument.DocumentType.InternalSubset;
				xmlDocument.RemoveChild(xmlDocument.DocumentType);
			}
			xmlDocument.InsertBefore(xmlDocument.DocumentElement, xmlDocument.CreateDocumentType(xmlDocument.DocumentElement.Name, publicId, dtdLocation, internalSubset));
		}

		public void SetSchemaLocation(string schemaLocation)
		{
			XmlDocument xmlDocument = (XmlDocument)base.Node;
			if (xmlDocument.DocumentElement == null)
			{
				throw new InvalidOperationException("SetSchemaLocation requires a root element.");
			}
			XmlAttribute xmlAttribute;
			if (xmlDocument.DocumentElement.NamespaceURI == "")
			{
				xmlAttribute = xmlDocument.CreateAttribute("xsi", "noNamespaceSchemaLocation", "http://www.w3.org/2001/XMLSchema-instance");
				xmlAttribute.Value = schemaLocation;
			}
			else
			{
				xmlAttribute = xmlDocument.CreateAttribute("xsi", "schemaLocation", "http://www.w3.org/2001/XMLSchema-instance");
				xmlAttribute.Value = xmlDocument.DocumentElement.NamespaceURI + " " + schemaLocation;
			}
			xmlDocument.DocumentElement.Attributes.Append(xmlAttribute);
		}

		public RDML_v1_2_REC2(XmlNode init)
			: base(init)
		{
			instantiateMembers();
		}

		private void instantiateMembers()
		{
			rdml2 = new MemberElement_rdml2(this, RDML_v1_2_REC_TypeInfo.binder.Members[RDML_v1_2_REC_TypeInfo._altova_mi_rdml_altova_RDML_v1_2_REC2_altova_rdml2]);
		}

		public void SetXsiType()
		{
			XmlTreeOperations.SetAttribute(base.Node, "xsi:type", "http://www.w3.org/2001/XMLSchema-instance", new XmlQualifiedName("RDML_v1_2_REC", "http://www.rdml.org"));
		}
	}
}
